import { getOrderList } from '@/api/order'

const getDefaultState = () => {
  return {
    // 分页
    pagination: {
      total: 0,
      currentPage: 1,
      limit: 10
    },
    // 表单值
    tableFrom: {
      status: -1,
      isDel: 0,
      mode: -1,
      dateLimit: '',
      keywords: '',
      type: '-1'
    },
    // 列表加载
    tableLoading: false,
    tableData: [],
    // 列表选中时
    selectionDate: []
  }
}

const state = getDefaultState()

const mutations = {
  SET_ORDER_TABLE_LOADING: (state, isLoading) => {
    state.tableLoading = isLoading
  },
  SET_ORDER_PAGE_TOTAL: (state, order) => {
    state.pagination.total = order
  },
  SET_ORDER_PAGE_CURRENT: (state, order) => {
    state.pagination.currentPage = order
  },
  // 修改state
  SET_ORDER_TABLE_FROM_STATUS: (state, value) => {
    state.tableFrom['status'] = value
  },
  // 修改mode
  SET_ORDER_TABLE_FROM_MODE: (state, value) => {
    state.tableFrom['mode'] = value
  },
  // 修改dateLimit
  SET_ORDER_TABLE_FROM_DATE_LIMIT: (state, value) => {
    state.tableFrom['dateLimit'] = value
  },
  // 修改keywords
  SET_ORDER_TABLE_FROM_KEYWORDS: (state, value) => {
    state.tableFrom['keywords'] = value
  },
  // 修改isDel
  SET_ORDER_TABLE_FROM_ISDEL: (state, value) => {
    state.tableFrom['isDel'] = value
  },
  SET_ORDER_TABLE_FROM_TYPE: (state, value) => {
    state.tableFrom['type'] = value
  },
  SET_ORDER_TABLE_DATA_SELECTION: (state, data) => {
    state.selectionDate = data
  }
}

const actions = {
  // 查询列表数据
  getList({ commit, state }) {
    commit('SET_ORDER_TABLE_LOADING', true)

    getOrderList({
      PageNumber: state.pagination.currentPage,
      PageSize: state.pagination.limit,
      // 支付类型
      PayType: state.tableFrom.mode,
      // 搜索关键字
      Keyword: state.tableFrom.keywords,
      // 状态值
      State: state.tableFrom.status === 9 ? -1 : state.tableFrom.status,
      // 已删除订单
      isDel: state.tableFrom.isDel,
      // 产品类型
      type: state.tableFrom.type,
      orderBy: 'createDateUTC desc'
    }).then(res => {
      const data = res.data
      commit('SET_ORDER_TABLE_LOADING', false)
      state.tableData = data
      commit('SET_ORDER_PAGE_TOTAL', res.head ? res.head.totalCount : 0)
    }).catch(() => {
      commit('SET_ORDER_TABLE_LOADING', false)
      state.tableData = []
      commit('SET_ORDER_PAGE_TOTAL', 0)
    })
  },
  // 获取订单数量
  getOrderCount({ commit }, order) {
    commit('SET_ORDER_COUNT', order)
  },
  setOrderPageTOTAL({ commit }, order) {
    commit('SET_ORDER_PAGE_TOTAL', order)
  },
  setOrderPageCurrentPage({ commit }, order) {
    commit('SET_ORDER_PAGE_CURRENT', order)
  },
  setOrderTableFromMode({ commit }, value) {
    commit('SET_ORDER_TABLE_FROM_MODE', value)
  },
  setOrderTableFromStatus({ commit }, value) {
    commit('SET_ORDER_TABLE_FROM_STATUS', value)
  },
  setOrderTableFromDateLimit({ commit }, value) {
    commit('SET_ORDER_TABLE_FROM_DATE_LIMIT', value)
  },
  setOrderTableFromKeywords({ commit }, value) {
    commit('SET_ORDER_TABLE_FROM_KEYWORDS', value)
  },
  setOrderTableFromIsDel({ commit }, value) {
    commit('SET_ORDER_TABLE_FROM_ISDEL', value)
  },
  setOrderTableFromType({ commit }, value) {
    commit('SET_ORDER_TABLE_FROM_TYPE', value)
  },
  setOrderTableDataSelection({ commit }, value) {
    commit('SET_ORDER_TABLE_DATA_SELECTION', value)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
